package com.woniu.cq.springboot_mybatisplus_shiro.comment;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 将所有的controller视作一个切面，提供一些功能
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    //做一个全局的异常处理
    @ExceptionHandler(Exception.class)
    public void handleException(Exception e, HttpServletResponse response){
        e.printStackTrace();

        try {
            response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println("出错了哦，快去找伟大的程序员吧"+e.getMessage());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
